from db.db_handler import MySQLHandler
from lib import common

login_dict_admin = {'username': '', 'role': ''}


def login(username, password):
    user_info = common.user_exists(username)
    if not user_info:
        return '【用户不存在】'
    else:
        if 'admin' not in user_info['role']:
            return f'【当前用户 {username} 权限不足！】'
        true_pwd = user_info['password']
        salt = user_info['password'][-4:]
        input_pwd = common.encrypt_data(password, salt)
        if true_pwd != input_pwd:
            return '【用户名或密码有误】'
        else:
            login_dict_admin['username'] = username
            login_dict_admin['role'] = 'admin'
            return '【登录成功】'


def register(username, password):
    user_info = common.user_exists(username)
    if user_info:
        return '【用户已存在,请不要重复注册！】'
    else:
        mysql_obj = MySQLHandler()
        salt = common.generate_code(4)
        password = common.encrypt_data(password, salt)
        res = mysql_obj.insert_data('account', username=username, password=password, role='admin')
        if res == 0:
            return '【注册失败，请联系系统管理员】'
        elif res == 1:
            return f'【用户 {username} 注册成功】'
        else:
            return '【发生了意料之外的错误，请联系系统管理员检查】'


def create_school(school_name, school_address):
    mysql_obj = MySQLHandler()
    res = mysql_obj.insert_data('school', school_name=school_name, address=school_address)
    if res == 0:
        return '【注册失败,请联系系统管理员】'
    elif res == 1:
        return f'【学校 {school_name} 注册成功】'
    else:
        return f'【注册失败 {res} 】'


def create_course(course_name, course_desc, course2school):
    mysql_obj = MySQLHandler()
    sql_search = 'select * from school where school_name=%(name)s'
    res = mysql_obj.search_one(sql_search, name=course2school)
    # res : {'id': 1, 'school_name': 'old boy', 'address': 'ShangHai'}
    if not res:
        return f'【学校 {course2school} 不存在】'
    res = mysql_obj.insert_data('course', course_name=course_name, course_desc=course_desc, school_id=res['id'])
    if res == 0:
        return '【创建失败,请联系系统管理员】'
    elif res == 1:
        return f'【课程 {course_name} 创建成功】'
    else:
        return f'【创建失败 {res} 】'


def create_teacher(name, age, sex, username, pwd, course):
    mysql_obj = MySQLHandler()
    search_sql = 'select * from course where course_name = %(name)s'
    course_info = mysql_obj.search_one(search_sql, name=course)
    if not course_info:
        return f'【课程 {course} 不存在】'
    salt = common.generate_code(4)
    encrypt_pwd = common.encrypt_data(data=pwd, salt=salt)
    teacher_sql = mysql_obj.insert_data('teacher', name=name, age=age, sex=sex)
    if teacher_sql != 1:
        return f'【讲师信息录入失败,请联系系统管理员 {teacher_sql}】'
    account_sql = mysql_obj.insert_data('account', username=username, password=encrypt_pwd,role='teacher')
    if account_sql != 1:
        return f'【讲师登录信息录入失败,请联系系统管理员 {account_sql}】'
    search_teacher_sql = 'select * from teacher where name = %(name)s'
    teacher_dict = mysql_obj.search_one(search_teacher_sql, name=name)
    res = mysql_obj.insert_data('course2teacher', course_id=course_info['id'], teacher_id=teacher_dict['id'])
    if res == 0:
        return '【创建失败,请联系系统管理员】'
    elif res == 1:
        return f'【讲师 {name} 创建成功】'
    else:
        mysql_obj.roll_back()
        return f'【创建失败 {res} 】'


def quit_system():
    login_dict_admin.clear()
